package com.ruoyi.web.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.MyCommodity;
import com.ruoyi.system.domain.MyConsumerData;
import com.ruoyi.system.service.IMyCommodityService;
import com.ruoyi.system.service.IMyConsumerDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MyOrder;
import com.ruoyi.system.service.IMyOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单信息Controller
 *
 * @author ruoyi
 * @date 2024-01-16
 */
@RestController
@RequestMapping("/system/order")
public class MyOrderController extends BaseController
{
    @Autowired
    private IMyOrderService myOrderService;

    @Autowired
    private IMyConsumerDataService myConsumerDataService;


    @Autowired
    private IMyCommodityService myCommodityService;
    /**
     * 查询订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyOrder myOrder)
    {
        startPage();
        List<MyOrder> list = myOrderService.selectMyOrderList(myOrder);
        return getDataTable(list);
    }

    /**
     * 查询顾客信息列表List
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/MyConsumerDataList")
    public TableDataInfo MyConsumerDataList(MyConsumerData myConsumerData)
    {
        List<MyConsumerData> list = myConsumerDataService.selectMyConsumerDataList(myConsumerData);
//        ArrayList<String> listStr =new ArrayList<>();
//        for (MyConsumerData myConsumerData1 : list) {
//            listStr.add(myConsumerData1.getConsumerName());
//        }
//        for (MyConsumerData it : list) {
//            it.setConsumerAddress(it.getConsumerName()+"-"+it.getConsumerAddress());
//        }
        return getDataTable(list);
    }

    /**
     * 查询商品信息列表List
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/MyCommodityList")
    public TableDataInfo MyCommodityList(MyCommodity myCommodity)
    {
        List<MyCommodity> list = myCommodityService.selectMyCommodityList(myCommodity);
        return getDataTable(list);
    }
    /**
     * 通过id查询商品信息列表List
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/MyConsumerDataList/{id}")
    public TableDataInfo MyConsumerDataById(@PathVariable("id") Long id)
    {
        MyConsumerData myConsumerData = myConsumerDataService.selectMyConsumerDataById(id);
        return getDataTable(Collections.singletonList(myConsumerData));
    }

    /**
     * 通过id查询用户信息列表List
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/MyCommodityList/{id}")
    public TableDataInfo MyCommodityById(@PathVariable("id") String id)
    {
        Long aLong = Long.valueOf(id);
        MyCommodity myCommodity = myCommodityService.selectMyCommodityById(aLong);
        return getDataTable(Collections.singletonList(myCommodity));
    }


    /**
     * 导出订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MyOrder myOrder)
    {
        List<MyOrder> list = myOrderService.selectMyOrderList(myOrder);
        ExcelUtil<MyOrder> util = new ExcelUtil<MyOrder>(MyOrder.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(myOrderService.selectMyOrderById(id));
    }

    /**
     * 新增订单信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyOrder myOrder)
    {
        return toAjax(myOrderService.insertMyOrder(myOrder));
    }

    /**
     * 修改订单信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyOrder myOrder)
    {
        return toAjax(myOrderService.updateMyOrder(myOrder));
    }

    /**
     * 删除订单信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(myOrderService.deleteMyOrderByIds(ids));
    }
}
